package com.education.expenses.web.controller;

import com.education.expenses.common.dto.ApproverReportsDTO;
import com.education.expenses.common.dto.GridDTO;
import com.education.expenses.common.dto.ResponseDTO;
import com.education.expenses.entity.Report;
import com.education.expenses.entity.ReportStatus;
import com.education.expenses.facade.ReportFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Егор
 * Date: 10.08.12
 * Time: 13:33
 */
@Controller
@RequestMapping(value = "/approver")
public class ApproverController {

   @Autowired
   ReportFacade reportFacade;

   @RequestMapping(value = "/reports")
   public @ResponseBody GridDTO<ApproverReportsDTO> reports(
          @RequestParam("rows") Integer rows,@RequestParam("page") Integer page)
   {

       GridDTO<ApproverReportsDTO> gridDTO = new GridDTO<ApproverReportsDTO>();
       gridDTO.setPageNumber(page.longValue());
       gridDTO.setPageCount(1L);
       gridDTO.setRecordsCount(2L);
       List<ApproverReportsDTO> list = new ArrayList<ApproverReportsDTO>();
       list.add(new ApproverReportsDTO(1,"egor shibeko","08/13/2012-08/19/2012","0","1000","0","0","0","1000","action"));
       gridDTO.setRows(list);
       return gridDTO;
   }

    @RequestMapping(value = "/report/approved",method = RequestMethod.POST)
    public @ResponseBody
    ResponseDTO approvedMark(@RequestParam("id") Integer id)
    {
        reportFacade.changeStatus(id, Enum.valueOf(ReportStatus.class,"APPROVED"));

        ResponseDTO response = new ResponseDTO();

        response.setSuccess(true);
        response.setMessage("Action successful");
        return response;
    }

    @RequestMapping(value = "/report/edit", method = RequestMethod.POST)
    public @ResponseBody ResponseDTO editReport(@RequestParam("id") Integer id,
                                       @RequestParam("lodging") Long lodging,
                                       @RequestParam("meals") Long meals,
                                       @RequestParam("transportation") Long transportation,
                                       @RequestParam("personal") Long personal,
                                       @RequestParam("misc") Long misc){

        reportFacade.updateExpenses(id,lodging,meals,transportation,personal,misc);

        ResponseDTO response = new ResponseDTO();
        response.setSuccess(true);
        response.setMessage("Action successful");

        return response;
    }
}
